/**
 * Metaphase Editor - WYSIWYG HTML Editor Component
 * Copyright (C) 2010  Rudolf Visagie
 * Full text of license can be found in com/metaphaseeditor/LICENSE.txt
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The author can be contacted at metaphase.editor@gmail.com.
 */

package com.metaphaseeditor;

import javax.swing.JOptionPane;

/**
 *
 * @author Rudolf Visagie
 */
public class ImageDialog extends javax.swing.JDialog {

    private enum Alignment {
        NONE("None", null),
        TOP("Top", "top"),
        BOTTOM("Bottom", "bottom"),
        MIDDLE("Middle", "middle"),
        LEFT("Left", "left"),
        RIGHT("Right", "right");

        private String text;
        private String attrValue;

        Alignment(String text, String attrValue) {
            this.text = text;
            this.attrValue = attrValue;
        }

        public String getAttrValue() {
            return attrValue;
        }

        public String getText() {
            return text;
        }

        @Override
        public String toString() {
            return text;
        }
    };

    private String imageHtml = null;
    private boolean link = false;
   
    /** Creates new form ImageDialog */
    public ImageDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/com/metaphaseeditor/icons/metaphase16x16.png")).getImage());

        setLocationRelativeTo(null);

        alignComboBox.removeAllItems();
        Alignment[] alignments = Alignment.values();
        for (int i=0; i<alignments.length; i++) {
            alignComboBox.addItem(alignments[i]);
        }

        languageDirectionComboBox.removeAllItems();
        LanguageDirection[] languageDirections = LanguageDirection.values();
        for (int i=0; i<languageDirections.length; i++) {
            languageDirectionComboBox.addItem(languageDirections[i]);
        }

        targetComboBox.removeAllItems();
        LinkTarget[] linkTargets = LinkTarget.values();
        for (int i=0; i<linkTargets.length; i++) {
            targetComboBox.addItem(linkTargets[i]);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        mainTabbedPane = new javax.swing.JTabbedPane();
        imageInfoPanel = new javax.swing.JPanel();
        urlLabel = new javax.swing.JLabel();
        urlTextField = new javax.swing.JTextField();
        alternativeTextLabel = new javax.swing.JLabel();
        alternativeTextTextField = new javax.swing.JTextField();
        widthLabel = new javax.swing.JLabel();
        widthSpinner = new javax.swing.JSpinner();
        heightLabel = new javax.swing.JLabel();
        heightSpinner = new javax.swing.JSpinner();
        borderLabel = new javax.swing.JLabel();
        borderSpinner = new javax.swing.JSpinner();
        hspaceLabel = new javax.swing.JLabel();
        hspaceSpinner = new javax.swing.JSpinner();
        vspaceLabel = new javax.swing.JLabel();
        vspaceSpinner = new javax.swing.JSpinner();
        alignLabel = new javax.swing.JLabel();
        alignComboBox = new javax.swing.JComboBox();
        widthToggleButton = new javax.swing.JToggleButton();
        heightToggleButton = new javax.swing.JToggleButton();
        mLinkPanel = new javax.swing.JPanel();
        linkUrlLabel = new javax.swing.JLabel();
        linkUrlTextField = new javax.swing.JTextField();
        targetLabel = new javax.swing.JLabel();
        targetComboBox = new javax.swing.JComboBox();
        advancedPanel = new javax.swing.JPanel();
        idTextField = new javax.swing.JTextField();
        idLabel = new javax.swing.JLabel();
        languageCodeLabel = new javax.swing.JLabel();
        languageCodeTextField = new javax.swing.JTextField();
        styleLabel = new javax.swing.JLabel();
        styleTextField = new javax.swing.JTextField();
        advisoryTitleTextField = new javax.swing.JTextField();
        advisoryTitleLabel = new javax.swing.JLabel();
        languageDirectionLabel = new javax.swing.JLabel();
        languageDirectionComboBox = new javax.swing.JComboBox();
        styleClassesLabel = new javax.swing.JLabel();
        styleClassesTextField = new javax.swing.JTextField();
        longDescLabel = new javax.swing.JLabel();
        longDescTextField = new javax.swing.JTextField();
        cancelButton = new javax.swing.JButton();
        okButton = new javax.swing.JButton();

        jLabel1.setText("jLabel1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Insert Image");

        urlLabel.setText("URL");

        alternativeTextLabel.setText("Alternative Text");

        widthLabel.setText("Width");

        widthSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
        widthSpinner.setEnabled(false);

        heightLabel.setText("Height");

        heightSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));
        heightSpinner.setEnabled(false);

        borderLabel.setText("Border");

        borderSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        hspaceLabel.setText("HSpace");

        hspaceSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        vspaceLabel.setText("VSpace");

        vspaceSpinner.setModel(new javax.swing.SpinnerNumberModel(Integer.valueOf(0), Integer.valueOf(0), null, Integer.valueOf(1)));

        alignLabel.setText("Align");

        alignComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "Top", "Bottom", "Middle", "Left", "Right" }));

        widthToggleButton.setSelected(true);
        widthToggleButton.setText("Default");
        widthToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                widthToggleButtonActionPerformed(evt);
            }
        });

        heightToggleButton.setSelected(true);
        heightToggleButton.setText("Default");
        heightToggleButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                heightToggleButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout imageInfoPanelLayout = new javax.swing.GroupLayout(imageInfoPanel);
        imageInfoPanel.setLayout(imageInfoPanelLayout);
        imageInfoPanelLayout.setHorizontalGroup(
            imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imageInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(urlLabel)
                    .addComponent(alternativeTextLabel)
                    .addComponent(widthLabel)
                    .addComponent(heightLabel)
                    .addComponent(borderLabel)
                    .addComponent(hspaceLabel)
                    .addComponent(vspaceLabel)
                    .addComponent(alignLabel))
                .addGap(31, 31, 31)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(alternativeTextTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                    .addComponent(urlTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                    .addComponent(alignComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(imageInfoPanelLayout.createSequentialGroup()
                        .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(vspaceSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(hspaceSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(borderSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(heightSpinner, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(widthSpinner, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(heightToggleButton)
                            .addComponent(widthToggleButton))))
                .addContainerGap())
        );
        imageInfoPanelLayout.setVerticalGroup(
            imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(imageInfoPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(urlLabel)
                    .addComponent(urlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alternativeTextLabel)
                    .addComponent(alternativeTextTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(widthLabel)
                    .addComponent(widthSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(widthToggleButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(heightLabel)
                    .addComponent(heightSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(heightToggleButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(borderLabel)
                    .addComponent(borderSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hspaceLabel)
                    .addComponent(hspaceSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(vspaceLabel)
                    .addComponent(vspaceSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(imageInfoPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(alignLabel)
                    .addComponent(alignComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(54, Short.MAX_VALUE))
        );

        mainTabbedPane.addTab("Image Info", imageInfoPanel);

        linkUrlLabel.setText("URL");

        targetLabel.setText("Target");

        targetComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None" }));

        javax.swing.GroupLayout mLinkPanelLayout = new javax.swing.GroupLayout(mLinkPanel);
        mLinkPanel.setLayout(mLinkPanelLayout);
        mLinkPanelLayout.setHorizontalGroup(
            mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mLinkPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(targetLabel)
                    .addComponent(linkUrlLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(linkUrlTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 333, Short.MAX_VALUE)
                    .addComponent(targetComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        mLinkPanelLayout.setVerticalGroup(
            mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mLinkPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(linkUrlLabel)
                    .addComponent(linkUrlTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mLinkPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(targetLabel)
                    .addComponent(targetComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(216, Short.MAX_VALUE))
        );

        mainTabbedPane.addTab("Link", mLinkPanel);

        idLabel.setText("ID");

        languageCodeLabel.setText("Language Code");

        styleLabel.setText("Style");

        advisoryTitleLabel.setText("Advisory Title");

        languageDirectionLabel.setText("Language Direction");

        languageDirectionComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "None", "Left to Right", "Right to Left" }));

        styleClassesLabel.setText("Style Classes");

        longDescLabel.setText("Long Description URL");

        javax.swing.GroupLayout advancedPanelLayout = new javax.swing.GroupLayout(advancedPanel);
        advancedPanel.setLayout(advancedPanelLayout);
        advancedPanelLayout.setHorizontalGroup(
            advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(advancedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(idLabel)
                    .addComponent(longDescLabel)
                    .addComponent(languageDirectionLabel)
                    .addComponent(advisoryTitleLabel)
                    .addComponent(styleClassesLabel)
                    .addComponent(styleLabel)
                    .addComponent(languageCodeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(languageCodeTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addComponent(styleTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addComponent(advisoryTitleTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addComponent(languageDirectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(styleClassesTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addComponent(idTextField, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE)
                    .addComponent(longDescTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 264, Short.MAX_VALUE))
                .addContainerGap())
        );
        advancedPanelLayout.setVerticalGroup(
            advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(advancedPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(idLabel)
                    .addComponent(idTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(longDescLabel)
                    .addComponent(longDescTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(styleClassesLabel)
                    .addComponent(styleClassesTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(styleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(styleLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(advisoryTitleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(advisoryTitleLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(languageDirectionComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(languageDirectionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(advancedPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(languageCodeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(languageCodeLabel))
                .addContainerGap(84, Short.MAX_VALUE))
        );

        mainTabbedPane.addTab("Advanced", advancedPanel);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(254, Short.MAX_VALUE)
                .addComponent(okButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cancelButton)
                .addContainerGap())
            .addComponent(mainTabbedPane, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cancelButton, okButton});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(mainTabbedPane, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(okButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void widthToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_widthToggleButtonActionPerformed
        widthSpinner.setEnabled(!widthSpinner.isEnabled());
    }//GEN-LAST:event_widthToggleButtonActionPerformed

    private void heightToggleButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_heightToggleButtonActionPerformed
        heightSpinner.setEnabled(!heightSpinner.isEnabled());
    }//GEN-LAST:event_heightToggleButtonActionPerformed

    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
        String url = urlTextField.getText();
        String alternativeText = alternativeTextTextField.getText();
        int width = ((Integer)widthSpinner.getValue()).intValue();
        int height = ((Integer)heightSpinner.getValue()).intValue();
        int border = ((Integer)borderSpinner.getValue()).intValue();
        int hspace = ((Integer)hspaceSpinner.getValue()).intValue();
        int vspace = ((Integer)vspaceSpinner.getValue()).intValue();
        Alignment alignment = (Alignment)alignComboBox.getSelectedItem();
        String linkUrl = linkUrlTextField.getText();
        LinkTarget linkTarget = (LinkTarget)targetComboBox.getSelectedItem();
        String id = idTextField.getText();
        String longDescUrl = longDescTextField.getText();
        String styleClasses = styleClassesTextField.getText();
        String style = styleTextField.getText();
        String advisoryTitle = advisoryTitleTextField.getText();
        LanguageDirection languageDirection = (LanguageDirection)languageDirectionComboBox.getSelectedItem();
        String languageCode = languageCodeTextField.getText();

        if (url.length() == 0) {
            JOptionPane.showMessageDialog(null, "The image URL must be specified.", "Error", JOptionPane.ERROR_MESSAGE);
            return;
        }

        StringBuffer buffer = new StringBuffer();
        if (linkUrl.length() > 0) {
            link = true;
            buffer.append("<a href='");
            buffer.append(linkUrl);
            buffer.append("'");

            if (linkTarget.getAttrValue() != null) {
                buffer.append(" target='");
                buffer.append(linkTarget.getAttrValue());
                buffer.append("'");
            }
            buffer.append('>');
        }

        buffer.append("<img src='");
        buffer.append(url);
        buffer.append("'");

        if (alternativeText.length() > 0) {
            buffer.append(" alt='");
            buffer.append(alternativeText);
            buffer.append("'");
        }
        if (!widthToggleButton.isSelected()) {
            buffer.append(" width='");
            buffer.append(width);
            buffer.append("'");
        }
        if (!heightToggleButton.isSelected()) {
            buffer.append(" height='");
            buffer.append(height);
            buffer.append("'");
        }

        buffer.append(" border='");
        buffer.append(border);
        buffer.append("'");

        buffer.append(" hspace='");
        buffer.append(hspace);
        buffer.append("'");

        buffer.append(" vspace='");
        buffer.append(vspace);
        buffer.append("'");

        if (alignment.getAttrValue() != null) {
            buffer.append(" align='");
            buffer.append(alignment.getAttrValue());
            buffer.append("'");
        }

        if (id.length() > 0) {
            buffer.append(" id='");
            buffer.append(id);
            buffer.append("'");
        }
        if (longDescUrl.length() > 0) {
            buffer.append(" longdesc='");
            buffer.append(longDescUrl);
            buffer.append("'");
        }
        if (styleClasses.length() > 0) {
            buffer.append(" class='");
            buffer.append(styleClasses);
            buffer.append("'");
        }
        if (style.length() > 0) {
            buffer.append(" style='");
            buffer.append(style);
            buffer.append("'");
        }
        if (advisoryTitle.length() > 0) {
            buffer.append(" title='");
            buffer.append(advisoryTitle);
            buffer.append("'");
        }
        if (languageCode.length() > 0) {
            buffer.append(" lang='");
            buffer.append(languageCode);
            buffer.append("'");
        }
        if (languageDirection.getAttrValue() != null) {
            buffer.append(" dir='");
            buffer.append(languageDirection.getAttrValue());
            buffer.append("'");
        }
        buffer.append("/>");

        if (link) {
            buffer.append("</a>");
        }
        imageHtml = buffer.toString();
        setVisible(false);
    }//GEN-LAST:event_okButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    public String showDialog() {
        setVisible(true);
        return imageHtml;
    }

    public boolean isLink() {
        return link;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel advancedPanel;
    private javax.swing.JLabel advisoryTitleLabel;
    private javax.swing.JTextField advisoryTitleTextField;
    private javax.swing.JComboBox alignComboBox;
    private javax.swing.JLabel alignLabel;
    private javax.swing.JLabel alternativeTextLabel;
    private javax.swing.JTextField alternativeTextTextField;
    private javax.swing.JLabel borderLabel;
    private javax.swing.JSpinner borderSpinner;
    private javax.swing.JButton cancelButton;
    private javax.swing.JLabel heightLabel;
    private javax.swing.JSpinner heightSpinner;
    private javax.swing.JToggleButton heightToggleButton;
    private javax.swing.JLabel hspaceLabel;
    private javax.swing.JSpinner hspaceSpinner;
    private javax.swing.JLabel idLabel;
    private javax.swing.JTextField idTextField;
    private javax.swing.JPanel imageInfoPanel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel languageCodeLabel;
    private javax.swing.JTextField languageCodeTextField;
    private javax.swing.JComboBox languageDirectionComboBox;
    private javax.swing.JLabel languageDirectionLabel;
    private javax.swing.JLabel linkUrlLabel;
    private javax.swing.JTextField linkUrlTextField;
    private javax.swing.JLabel longDescLabel;
    private javax.swing.JTextField longDescTextField;
    private javax.swing.JPanel mLinkPanel;
    private javax.swing.JTabbedPane mainTabbedPane;
    private javax.swing.JButton okButton;
    private javax.swing.JLabel styleClassesLabel;
    private javax.swing.JTextField styleClassesTextField;
    private javax.swing.JLabel styleLabel;
    private javax.swing.JTextField styleTextField;
    private javax.swing.JComboBox targetComboBox;
    private javax.swing.JLabel targetLabel;
    private javax.swing.JLabel urlLabel;
    private javax.swing.JTextField urlTextField;
    private javax.swing.JLabel vspaceLabel;
    private javax.swing.JSpinner vspaceSpinner;
    private javax.swing.JLabel widthLabel;
    private javax.swing.JSpinner widthSpinner;
    private javax.swing.JToggleButton widthToggleButton;
    // End of variables declaration//GEN-END:variables

}
